package com.petecapra.cashflow.model.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.petecapra.cashflow.model.Account;
import com.petecapra.cashflow.model.AccountTransaction;

@Service
public class AccountTransactionManager extends CashflowManager<AccountTransaction> {

	public AccountTransactionManager() {
		super(AccountTransaction.class);		
	}
	
	@Override
	public void delete(AccountTransaction at) {
		at.getSchedule().getAccountTransactions().remove(at);	
		super.delete(at);
	}

	@SuppressWarnings("unchecked")
	public List<AccountTransaction> getUnpaidTransactions(Account account) {
		Criteria criteria = getCriteria();
		
		// only return transactions that are not paid
		criteria.add(
			Restrictions.eq("paid", false));
		
		// only return transactions for this account
		criteria.createCriteria("schedule").add(
			Restrictions.eq("account", account));
		
		// return transactions ordered by due date
		criteria.addOrder(
			Order.asc("due"));
		
		return criteria.list();		
	}

}